package com.cswnode.Controller;

import com.cswnode.Entity.User;
import com.cswnode.Service.RegisteredService;
import com.cswnode.Util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class RegisteredController {

    @Autowired
    private RegisteredService registeredService;

    @GetMapping("/ToRegistered")
    public String toRegisteredPage(){
        return "/site/register";
    }

    /**
     * 验证用户是否存在
     * @param username
     * @return
     */
    @ResponseBody
    @PostMapping("/judgeUsername")
    public Map<String,Boolean> JudgeUsername(String username){
        int user = registeredService.JudgeUsername(username);
        Map<String,Boolean> map = new HashMap<>();
        if (user!=0){
            map.put("success",true);
        }else {
            map.put("success",false);
        }
        return map;
    }

    /**
     * 验证邮箱是否被注册
     * @param Email
     * @return
     */
    @ResponseBody
    @PostMapping("/judgeEmail")
    public Map<String,Boolean> JudgeEmail(String Email){
        Boolean i =  registeredService.JudgeEmail(Email);
        Map<String,Boolean> map = new HashMap<>();
        map.put("success",i);
        return map;
    }

    @PostMapping("/registered")
    public String Registered(Model model, User user){
        Map<String, Object> map = registeredService.RegisterdUser(user);
        if (map == null || map.isEmpty()){
            model.addAttribute("msg","注册成功！我们已向您的邮箱发送了一封激活邮件请注意查收");
            model.addAttribute("target","/community/");
            return "/site/operate-result";
        }else {
            model.addAttribute("usermsg",map.get("usermsg"));
            model.addAttribute("passwordmsg",map.get("passwordmsg"));
            model.addAttribute("Emailmsg",map.get("Emailmsg"));
            return "/site/register";
        }
    }
    @GetMapping("/activation/{userId}/{code}")
    public String ActivationUser(Model model,@PathVariable("userId")int userId,@PathVariable("code")String code){
        int result;
        result = registeredService.ActivationUser(userId,code);
        switch (result){
            case Constant.ACTIVATION_SUCCESS:
                model.addAttribute("msg","用户激活成功！可以正常使用了");
                model.addAttribute("target","/community/login");
                return "/site/operate-result";
            case Constant.ACTIVATION_REPEAT:
                model.addAttribute("msg","该用户已激活，请勿重复激活！");
                model.addAttribute("target","/community/");
                return "/site/operate-result";
            default:
                model.addAttribute("msg","用户激活失败，请稍后再试");
                model.addAttribute("target","/community/");
                return "/site/operate-result";
        }
    }



}
